package com.deyuanyun.pic.pipe.service.impl;

import com.deyuanyun.pic.common.dto.DTOResult;
import com.deyuanyun.pic.common.exception.ExceptionCode;
import com.deyuanyun.pic.common.exception.ExceptionCodeUtil;
import com.deyuanyun.pic.common.exception.bizcode.basedata.pipe.PipeValveCodeEnum;
import com.deyuanyun.pic.common.exception.code.ModuleMsg;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.pipe.mapper.dao.ValveDao;
import com.deyuanyun.pic.pipe.service.PipeValveService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class PipeValveServiceImpl implements PipeValveService {

	@Resource
	private ValveDao valveDao;

	@Override
	public DTOResult getPipeValveByParam(String pipeLineId,
										 String pipeSegmentId, BigDecimal startMileage,
										 BigDecimal endMileage, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub

		int page = 0;

		int size = 0;
		// 初始化当前页数
		if (pageNum == null) {
			page = 0;
		} else {
			page = pageNum;
		}
		// 初始化每页显示数量
		if (pageSize == null) {
			size = 20;
		} else {
			size = pageSize;
		}

		PageHelper.startPage(page, size);

		String code = "";

		if (null == pipeLineId || "".equals(pipeLineId)) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeValveCodeEnum.pipeLinedIdIsNull.getCode(),
					"未选择管线");
		}

		if (null == pipeSegmentId || "".equals(pipeSegmentId)) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeValveCodeEnum.pipeSegmentIdIsNull.getCode(),
					"未选择管线");
		}

		// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (endMileage != null && "".equals(endMileage)) {
			mileageEnd = endMileage;
		}

		// 定义一个管段ID
		String pipeSegmentIdStr = "";

		if (!"all".equals(pipeSegmentId)) {
			pipeSegmentIdStr = pipeSegmentId;
		}

		Page<HashMap<String, Object>> map = null;

		// 在DAO层获取一个MAP集合对象
		map = valveDao.selectBaseByParam(pipeLineId, pipeSegmentIdStr,
				mileageStart, mileageEnd);

		if (map == null) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeValveCodeEnum.mapperIsNull.getCode(),
					"没有查询到结果集");
		}

		if (map.size() <= 0) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeValveCodeEnum.mapperIsNull.getCode(),
					"没有查询到结果集");
		}

		return new DTOResult(ExceptionCode.SUCCESS.getCode(), "正常", map);

	}

	@Override
	public List<List<String[]>> getPipeValveToExcel(String pipeLineId,
			String pipeSegmentId, BigDecimal startMileage, BigDecimal endMileage) {

		
		// 定义一个返回值
		List<List<String[]>> result = new ArrayList<List<String[]>>();

		/*// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (endMileage != null && !"".equals(endMileage)) {
			mileageEnd = endMileage;
		}
*/
		// 定义一个管段ID
		String pipeSegmentIdStr = "";

		if (!"all".equals(pipeSegmentId)) {
			pipeSegmentIdStr = pipeSegmentId;
		}

		List<LinkedHashMap<String, Object>> list = valveDao
				.selectValveToExcel(pipeLineId, pipeSegmentIdStr, startMileage,
						endMileage);
		List<LinkedHashMap<String, Object>> list2=new ArrayList<LinkedHashMap<String, Object>>();

		for(int i=0;i<list.size();i++){
			LinkedHashMap<String, Object> map=list.get(i);
			map.put("VALVE_STATUS", CodeUtil.getName((String) map.get("VALVE_STATUS")));
			map.put("VALVE_TYPE", CodeUtil.getName((String) map.get("VALVE_TYPE")));
			map.put("OPERATOR_TYPE", CodeUtil.getName((String) map.get("OPERATOR_TYPE")));
			map.put("AUTOMATED", CodeUtil.getName((String) map.get("AUTOMATED")));
			map.put("INLET_CONNECTION_TYPE", CodeUtil.getName((String) map.get("INLET_CONNECTION_TYPE")));
			map.put("OUTLET_CONNECTION_TYPE", CodeUtil.getName((String) map.get("OUTLET_CONNECTION_TYPE")));
			map.put("VALVEFUNCTION", CodeUtil.getName((String) map.get("VALVEFUNCTION")));
//			map.put("INDREMOTECON", CodeUtil.getName((String) map.get("INDREMOTECON")));
			map.put("REPAIR_SUGGESTIONG", CodeUtil.getName((String) map.get("REPAIR_SUGGESTIONG")));
			map.put("REPAIR_STATE", CodeUtil.getName((String) map.get("REPAIR_STATE")));
			list2.add(map);
		}
		
		
		
		if (list2.size() == 0) {
			list2 = null;
		}

		result.add(JXL.toList(list2, "yyyy-MM-dd HH:mm:ss"));

		return result;
	}


	@Override
	public Page<HashMap<String, Object>> getDetailed(String pipeLineId,
			String pipeSegmentId, BigDecimal startMileage,
			BigDecimal endMileage, Integer pageNum, Integer pageSize) {

		
		int page = 1;

		int size = 10;

		if (null != pageNum ) {
			page = pageNum;
		}

		if (null != pageSize ) {
			size = pageSize;
		}

		PageHelper.startPage(page,size);
		
		
		/*// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (endMileage != null && !"".equals(endMileage)) {
			mileageEnd = endMileage;
		}*/
		Page<HashMap<String, Object>> list = new Page<HashMap<String, Object>>();
		Page<HashMap<String, Object>> list2 = new Page<HashMap<String, Object>>();

		list= valveDao.selectValveDetailed(pipeLineId, pipeSegmentId, startMileage, endMileage);
		for (int i=0;i<list.size();i++){
			HashMap map=list.get(i);
			map.put("VALVE_STATUS", CodeUtil.getName((String) map.get("VALVE_STATUS")));
			map.put("VALVE_TYPE", CodeUtil.getName((String) map.get("VALVE_TYPE")));
			map.put("OPERATOR_TYPE", CodeUtil.getName((String) map.get("OPERATOR_TYPE")));
			map.put("AUTOMATED", CodeUtil.getName((String) map.get("AUTOMATED")));
			map.put("INLET_CONNECTION_TYPE", CodeUtil.getName((String) map.get("INLET_CONNECTION_TYPE")));
			map.put("OUTLET_CONNECTION_TYPE", CodeUtil.getName((String) map.get("OUTLET_CONNECTION_TYPE")));
			map.put("VALVEFUNCTION", CodeUtil.getName((String) map.get("VALVEFUNCTION")));
//			map.put("INDREMOTECON", CodeUtil.getName((String) map.get("INDREMOTECON")));
			map.put("REPAIR_SUGGESTIONG", CodeUtil.getName((String) map.get("REPAIR_SUGGESTIONG")));
			map.put("REPAIR_STATE", CodeUtil.getName((String) map.get("REPAIR_STATE")));
			list2.add(map);
		}
		list2.setTotal(list.getTotal());
		list2.setPageNum(list.getPageNum());
		list2.setPageSize(list.getPageSize());
		
		
		
		return list2;
	}

}
